/**
 * 
 */
package com.mvc.service.healthmanager.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.huangqj.consant.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mvc.dao.BaseDaoI;
import com.mvc.dao.mapper.TemperatureDao;
import com.mvc.framework.constant.GlobalConstant;
import com.mvc.model.healthmanager.archive.BloodOxygen;
import com.mvc.model.healthmanager.archive.Temperature;
import com.mvc.model.healthmanager.archive.UserTemperature;
import com.mvc.service.healthmanager.TemperatureServiceI;
import com.mvc.utils.CreatHighChartJson;
import com.mvc.utils.StringUtil;

/**
 * @author qingjie
 *
 */
@Service
public class TemperatureServiceImpl implements TemperatureServiceI {
	
	private final String titleName = "体温数据走势";
	private final String unit = "℃";
	private final String yAxixname = "体温(℃)";
	
	@Autowired
	private TemperatureDao tDao;
	@Autowired
	private BaseDaoI<Temperature> tDaoI;
	@Autowired
	private BaseDaoI<UserTemperature> utDaoI;
	@Override
	public Temperature getNewTemperature(Temperature t) {
		Map<String, Object> params = StringUtils.beanToMap(t);
		List<Temperature> list = tDaoI.find(StringUtil.getHql("Temperature", params, "desc"), params);
		return list.size()>0?list.get(0):new Temperature();
	}

	
	@Override
	public List<Temperature> getTemperature(Temperature tem) {
		return tDao.getTemperature(tem);
	}

	
	@Override
	public String getJsonData(String personId) {
		List<Temperature> list = tDao.getJsonData(personId);
		String[] xAxis = new String[list.size()];
		Map<String, Object> map = new HashMap<String, Object>();
		if(!list.isEmpty()){
			Map<String, Object> tem_m = new HashMap<String, Object>();
			tem_m.put("name", "体温(℃)");
			List<Double> tem_l = new ArrayList<Double>();
			
			for(int i=0;i<list.size();i++){
				Temperature tem = list.get(i);
				xAxis[i] = GlobalConstant.SDF.format(tem.getCreatDate());
				tem_l.add(i, tem.getTemperature());
			}
			tem_m.put("data", tem_l);
			map.put("temperature", tem_m);
		}
		return CreatHighChartJson.CreatJsonData(titleName, unit, yAxixname, xAxis, map);
	}


	@Override
	public List<UserTemperature> findTemperature(String personId) {
		List<UserTemperature> ut = utDaoI.find("from UserTemperature where personId =  '"+personId+"'");
		return ut;
	}


	@Override
	public Temperature saveOrUpdate(Temperature temperature) {
		int count = 1;
		List<Temperature> list = tDao.getTemperature(temperature);
		if(list.size()>0){
			Temperature t = list.get(list.size()-1);
			count = t.getCount()+1;
		}
		temperature.setCount(count);
		temperature.setModifyDate(new Date());
		tDaoI.saveOrUpdate(temperature);
		return temperature;
	}

}
